/*
		C++作业
		实现复数的加减乘除

		夏思成

		西科大15号公寓楼
*/
#include <iostream>
using namespace std;

class plural
{
public:
    //构造函数 
    plural(float x = 0, float y = 0)
    {
        real = x;
        imag = y;
    }
    //析构函数
    ~plural()
    {
        //cout<<"plural deleted"<<endl;
    }

    //加法函数 
    static plural Add(plural p1, plural p2)
    {
        plural c;
        c.real = p1.real + p2.real;
        c.imag = p1.imag + p2.imag;
        return  c;
    }

    //减法函数
    static plural Min(plural p1, plural p2)
    {
        plural c;
        c.real = p1.real - p2.real;
        c.imag = p1.imag - p2.imag;
        return  c;
    }

    //乘法函数
    static plural Mul(plural p1, plural p2)
    {
        plural c;
        c.real = p1.real * p2.real;
        c.imag = p1.imag * p2.imag;
        return  c;
    }

    //除法函数
    static plural Div(plural p1, plural p2)
    {
        if (p2.real == 0 && p2.imag == 0)
        {
            cout << "不能进行除法运算,除数不能为0" << endl;
            return plural();
        }
        plural c;
        float denominator = p2.real * p2.real + p2.imag * p2.imag;
        c.real = (p1.real * p2.real + p1.imag * p2.imag) / denominator;
        c.imag = (p2.imag * p2.real - p2.real * p2.imag) / denominator;
        return  c;
    }
    void calc(plural(*pf)(plural, plural))
    {
        float ar = 0;//第一个复数的实部
        float ai = 0;//第一个复数的虚部
        float br = 0;//第二个复数的实部
        float bi = 0;//第二个复数的虚部
        //创建plural型变量
        cout << "请输入第一个复数的实部和虚部" << endl;
        cin >> ar >> ai;
        cout << "请输入第2个复数的实部和虚部" << endl;
        cin >> br >> bi;
        plural a(ar, ai);
        plural b(br, bi);
        plural c = pf(a, b);
        cout << "计算结果为:" << endl;
        cout << c.real << "," << c.imag << endl;
    }
private:
    float real;//复数实部 
    float imag;//复数虚部 
};

//菜单函数
void Print_menu()
{
    cout << "******1.Add******" << endl;
    cout << "******2.Min******" << endl;
    cout << "******3.Mul*******" << endl;
    cout << "******4.Div*******" << endl;
    cout << "******0.exit******" << endl;
}

void test()
{
    plural c;
    int n = 0;
    do
    {
        //菜单打印 
        Print_menu();

        //模式选择 
        cin >> n;

        switch (n)
        {
        case 1:
            c.calc(plural::Add);
            break;
        case 2:
            c.calc(plural::Min);
            break;
        case 3:
            c.calc(plural::Mul);
            break;
        case 4:
            c.calc(plural::Div);
            break;
        case 0:
            cout << "退出复数计算器" << endl;
            break;
        default:
            cout << "非法输入，请重新选择" << endl;
            break;
        }
    } while (n);
}

int main()
{
    test();
    return 0;
}